﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Extensil.Processor;
using System.IO;
using System.Diagnostics;
using Extensil.Processor.Extensibility;

namespace Extensil.CmdProcessor
{
    public class Program
    {
        class Abbreviation
        {
            public const string PluginPath = "pluginPath";
        }
        static bool _firstTimeExecution = true;
        static Dictionary<string, string> cmdArgs;
        static Arguments CommandLine;
        static void Main(string[] args)
        {
            try
            {
                ConfigSystem.Install();
                if(args.Length == 0)
                {
                    if(_firstTimeExecution)
                        Process.Start("cmd");
                    return;
                }
                if(args.Length == 1)
                {
                    switch(args.GetValue(0).ToString().ToLower())
                    {
                        case "help":
                        case "?":
                        case "/":
                            ShowWelcome();
                            ShowHelp();
                            return;
                        default:
                            break;
                    }
                }
                CommandLine = new Arguments(args);

                if(CommandLine[Abbreviation.PluginPath] != null)
                {
                    try
                    {
                        if(!File.Exists(CommandLine[Abbreviation.PluginPath]))
                        {
                            throw new Exception("Invalid plugin path " + CommandLine[Abbreviation.PluginPath] + ".");
                        }
                        PluginManager pm = new PluginManager(CommandLine[Abbreviation.PluginPath], ReportStatus);
                        InstallResult res = pm.Install();
                        if(res.IsSuccessFull)
                            Console.WriteLine(CommandLine[Abbreviation.PluginPath] + " installed successfully.");
                        else
                        {
                            foreach(Exception exc in res)
                            {
                                Console.WriteLine(exc.Message);
                            }
                            Console.WriteLine(CommandLine[Abbreviation.PluginPath] + " could not be installed.");
                        }
                    }
                    catch(Exception exc)
                    {
                        Console.WriteLine(exc.Message + "\r\n\t" + exc.StackTrace);
                    }
                    return;
                }
            }
            catch(Exception exc)
            {
                Console.Write(exc.Message);
            }
            finally
            {
                _firstTimeExecution = false;
            }
        }

        private static void ShowHelp()
        {
            System.Console.WriteLine("Syntax:");
            System.Console.WriteLine("  xtensil -pluginPath='Plugin path'");
            System.Console.WriteLine(" ");
            System.Console.WriteLine("     Plugin path: Location of the plugin to install.");
        }

        private static void ShowWelcome()
        {
            string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            System.Console.WriteLine("***************************************************");
            System.Console.WriteLine("*                                                 *");
            System.Console.WriteLine("*               Welcome to Extensil               *");
            System.Console.WriteLine("*                                                 *");
            System.Console.WriteLine("***************************************************");
            System.Console.WriteLine("Version: " + version);
        }
        public static void ReportStatus(int progress, string message)
        {
            Console.WriteLine(message);
        }
    }
}
